﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class Main_Opinion : System.Web.UI.Page
{
    EncriptManager em = new EncriptManager();
    NewsDao nd = new NewsDao();
    OpinionDao od = new OpinionDao();
    ReplyDao rd = new ReplyDao();
    DataTable dt = null;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            try
            {
                if (em.Decript(Request.QueryString["nid"]) == null)
                {
                    Response.Redirect("../index.aspx");
                }
            }
            catch
            {
                Response.Redirect("../index.aspx");
            }
            if (Request.QueryString["nid"] == "0")
            {
                lbtitle.Text = "站长留言板";
                dt = od.getMasterOpinion();
                btnreturn.Visible = false;
            }
            else
            {
                string nid = em.Decript(Request.QueryString["nid"]);
                try
                {
                    int id = Convert.ToInt32(nid);
                }
                catch
                {
                    Response.Redirect("../index.aspx");
                }
                NewsVo nv = new NewsVo();
                nv = nd.getSingleNews(nid);
                if (nv != null)
                {
                    lbtitle.Text = nv.Nmaintitle;
                }
                dt = od.getOpinion(nid, "0");
                if (dt.Rows.Count > 0)
                {
                    labnumber.Text = dt.Rows.Count.ToString();
                }
                else
                {
                    string script = "<script>alert('暂无任何留言！');location.href='News.aspx?nid=" + Request.QueryString["nid"] + "';</script>";
                    Page.RegisterClientScriptBlock("", script);
                }
            }
            if (Session["user"] != null)
            {
                UsersVo uv = new UsersVo();
                uv = (UsersVo)Session["user"];
                if (uv != null)
                {
                    labname.Visible = false;
                    txtname.Visible = false;
                }
            }
            if (ViewState["pageindex"] == null)
            {
                ViewState["pageindex"] = "0";
                lbpre.Visible = false;
                lbnext.Visible = false;
            }
            BindData();
        }
    }

    private void BindData()
    {
        string nid = em.Decript(Request.QueryString["nid"]);
        DataTable objTable = new DataTable();
        objTable = od.getOpinion(nid, "0");
        DataTable datatable = new DataTable("objTable");
        datatable.Columns.Add("留言id");
        datatable.Columns.Add("留言内容");
        datatable.Columns.Add("留言时间");
        datatable.Columns.Add("留言人");
        DataRow dr;
        if (objTable.Rows.Count > 0)
        {
            for (int i = 0; i < objTable.Rows.Count; i++)
            {
                dr = datatable.NewRow();
                dr["留言id"] = objTable.Rows[i][0].ToString();
                dr["留言内容"] = objTable.Rows[i][1].ToString();
                dr["留言时间"] = objTable.Rows[i][2].ToString();
                dr["留言人"] = objTable.Rows[i][4].ToString();
                datatable.Rows.Add(dr);
            }
        }
        if (datatable != null && datatable.Rows.Count > 0)
        {
            DataView objView = datatable.DefaultView;
            PagedDataSource objPds = new PagedDataSource();
            objPds.DataSource = objView;
            objPds.AllowPaging = true;
            objPds.PageSize = 20;
            objPds.CurrentPageIndex = int.Parse(ViewState["pageindex"].ToString());
            if (!objPds.IsFirstPage)
            {
                lbpre.Visible = true;
            }
            else
            {
                lbpre.Visible = false;
            }
            if (!objPds.IsLastPage)
            {
                lbnext.Visible = true;
            }
            else
            {
                lbnext.Visible = false;
            }
            dlopinion.DataSource = objPds;
            dlopinion.DataBind();
            for (int i = 0; i < dlopinion.Items.Count; i++)
            {
                Label lab = (Label)dlopinion.Items[i].FindControl("lbshijian");
                DateTime t = Convert.ToDateTime(lab.Text);
                lab.Text = t.ToString("g");
                LinkButton lb = (LinkButton)dlopinion.Items[i].FindControl("lbtndel");
                UsersVo uv = new UsersVo();
                uv = (UsersVo)Session["user"];
                if (uv != null)
                {
                    if (uv.Ugroup == "管理员")
                    {
                        lb.Visible = true;
                    }
                }
                //绑定回复表
                DataList dlreply = (DataList)dlopinion.Items[i].FindControl("dlreply");
                Label lblID = (Label)dlopinion.Items[i].FindControl("lblID");
                string oid = lblID.Text;
                dt = rd.getReply(oid);
                dlreply.DataSource = dt;
                dlreply.DataBind();
                for (int j = 0; j < dlreply.Items.Count; j++)
                {
                    LinkButton lbr = (LinkButton)dlreply.Items[j].FindControl("lbtndel");
                    if (uv != null)
                    {
                        if (uv.Ugroup == "管理员")
                        {
                            lbr.Visible = true;
                        }
                    }
                }
            }
            labnumber.Text = dlopinion.Items.Count.ToString();
        }
        else
        {
            lbpre.Visible = false;
            lbnext.Visible = false;
        }
    }

    protected void IndexChanging(object sender, EventArgs e)
    {
        string strCommand = ((LinkButton)sender).CommandArgument.ToString();
        int pageindex = int.Parse(ViewState["pageindex"].ToString());
        if (strCommand == "pre")
        {
            pageindex = pageindex - 1;
        }
        else
        {
            pageindex = pageindex + 1;
        }
        ViewState["pageindex"] = pageindex;
        BindData();
    }

    protected void dlopinion_ItemCommand(object source, DataListCommandEventArgs e)
    {
        string Nid = Request.QueryString["nid"];
        string type = e.CommandName;
        int index = e.Item.ItemIndex;
        if (type == "show")
        {
            if (Session["user"] != null)
            {
                UsersVo uv = new UsersVo();
                uv = (UsersVo)Session["user"];
                Panel plreply = e.Item.FindControl("plreply") as Panel;
                plreply.Visible = true;
                LinkButton lbtnhidereply = e.Item.FindControl("lbtnhidereply") as LinkButton;
                lbtnhidereply.Visible = true;
                LinkButton lbtnshowreply = e.Item.FindControl("lbtnshowreply") as LinkButton;
                lbtnshowreply.Visible = false;
            }
            else
            {
                string script = "alert('请先登录！');";
                ScriptManager.RegisterClientScriptBlock(UpdatePanel1, this.GetType(), "click", script, true);
            }
        }
        if (type == "hide")
        {
            Panel plreply = e.Item.FindControl("plreply") as Panel;
            plreply.Visible = false;
            LinkButton lbtnhidereply = e.Item.FindControl("lbtnhidereply") as LinkButton;
            lbtnhidereply.Visible = false;
            LinkButton lbtnshowreply = e.Item.FindControl("lbtnshowreply") as LinkButton;
            lbtnshowreply.Visible = true;
        }
        if (type == "add")
        {
            Label lblID = e.Item.FindControl("lblID") as Label;
            TextBox txtreply = e.Item.FindControl("txtreply") as TextBox;
            string script = "";
            if (txtreply.Text != "")
            {
                //添加回复
                ReplyVo rv = new ReplyVo();
                rv.Rcontent = txtreply.Text.Replace(" ", "&nbsp;&nbsp;&nbsp;").Replace("\r\n", "<br>");
                rv.Oid = Convert.ToInt32(lblID.Text);
                
                if (Session["user"] != null)
                {
                    UsersVo uv = new UsersVo();
                    uv = (UsersVo)Session["user"];
                    if (uv != null)
                    {
                        rv.Uid = uv.Uid;
                        rv.Rauthor = uv.Unickname;
                    }
                }
                Exception ex = rd.addReply(rv);
                if (ex == null)
                {
                    script = "alert('提交成功！');";
                    ScriptManager.RegisterClientScriptBlock(UpdatePanel1, this.GetType(), "click", script, true);
                    BindData();
                }
                else
                {
                    script = "alert('提交失败！');";
                    ScriptManager.RegisterClientScriptBlock(UpdatePanel1, this.GetType(), "click", script, true);
                }
            }
            else
            {
                script = "alert('提交不能为空！');";
                ScriptManager.RegisterClientScriptBlock(UpdatePanel1, this.GetType(), "click", script, true);
            }
        }
        if (type == "delete")
        {
            Label lblID = e.Item.FindControl("lblID") as Label;
            string oid = lblID.Text;
            Exception ex = new OpinionDao().delOpinion(oid);
            string script = "";
            if (ex == null)
            {
                script = "alert('删除成功！');";
                ScriptManager.RegisterClientScriptBlock(UpdatePanel1, this.GetType(), "click", script, true);
                BindData();
            }
            else
            {
                script = "alert('删除失败！');";
                ScriptManager.RegisterClientScriptBlock(UpdatePanel1, this.GetType(), "click", script, true);
            }
        }
    }
    protected void btntijiao_Click(object sender, EventArgs e)
    {
        string nid = em.Decript(Request.QueryString["nid"]);
        string Nid = Request.QueryString["nid"];
        if (txtcontent.Text == "")
        {
            string script = "alert('内容不能为空！');";
            ScriptManager.RegisterClientScriptBlock(UpdatePanel1, this.GetType(), "click", script, true);
        }
        else
        {
            OpinionVo ov = new OpinionVo();
            ov.Ocontent = txtcontent.Text.Replace(" ", "&nbsp;&nbsp;&nbsp;").Replace("\r\n", "<br>");
            if (Request.QueryString["nid"]=="0")
            {
                ov.Id = 0;
            }
            else
            {
                ov.Id = Convert.ToInt32(nid);
            }
            if (Session["user"] != null)
            {
                UsersVo uv = new UsersVo();
                uv = (UsersVo)Session["user"];
                if (uv != null)
                {
                    ov.Uid = uv.Uid;
                    ov.Oauthor = uv.Unickname;
                }
            }
            else
            {
                ov.Uid = 0;
                if (txtname.Text == "")
                {
                    ov.Oauthor = "纺织城网友";
                }
                else
                {
                    ov.Oauthor = txtname.Text;
                }
            }
            ov.Otype = 0;
            Exception ex = od.addOpinion(ov);
            if (ex == null)
            {
                string script = "alert('评论成功！');";
                ScriptManager.RegisterClientScriptBlock(UpdatePanel1, this.GetType(), "click", script, true);
                txtcontent.Text = "";
                txtname.Text = "";
                BindData();
            }
            else
            {
                string script = "alert('评论失败！');";
                ScriptManager.RegisterClientScriptBlock(UpdatePanel1, this.GetType(), "click", script, true);
            }
        }
    }
    protected void btnreturn_Click(object sender, EventArgs e)
    {
        string Nid = Request.QueryString["nid"];
        Response.Redirect("News.aspx?nid=" + Nid + "");
    }
    protected void dlreply_ItemCommand(object source, DataListCommandEventArgs e)
    {
        string type = e.CommandName;
        int index = e.Item.ItemIndex;
        if (type == "delete")
        {
            Label lblID = e.Item.FindControl("lblID") as Label;
            string rid = lblID.Text;
            Exception ex = rd.delReply(rid);
            string script = "";
            if (ex == null)
            {
                script = "alert('删除成功！');";
                ScriptManager.RegisterClientScriptBlock(UpdatePanel1, this.GetType(), "click", script, true);
                BindData();
            }
            else
            {
                script = "alert('删除失败！');";
                ScriptManager.RegisterClientScriptBlock(UpdatePanel1, this.GetType(), "click", script, true);
            }
        }
    }
}
